package com.offics.loan.application.service;

import com.offics.loan.application.dto.faq.FAQResponse;
import com.offics.loan.domain.entity.FAQ;
import com.offics.loan.domain.service.FAQDomainService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * FAQ应用服务
 */
@Service
@RequiredArgsConstructor
public class FAQApplicationService {

    private final FAQDomainService faqDomainService;

    /**
     * 获取所有启用的FAQ列表
     *
     * @return FAQ列表
     */
    public List<FAQResponse> getAllEnabledFAQs() {
        List<FAQ> faqs = faqDomainService.getAllEnabledFAQs();
        return faqs.stream()
                .map(this::convertToResponse)
                .collect(Collectors.toList());
    }

    /**
     * 根据分类获取启用的FAQ列表
     *
     * @param category 分类
     * @return FAQ列表
     */
    public List<FAQResponse> getFAQsByCategory(Integer category) {
        List<FAQ> faqs = faqDomainService.getFAQsByCategory(category);
        return faqs.stream()
                .map(this::convertToResponse)
                .collect(Collectors.toList());
    }

    /**
     * 根据ID获取FAQ详情
     *
     * @param id FAQ ID
     * @return FAQ详情
     */
    public FAQResponse getFAQById(Long id) {
        FAQ faq = faqDomainService.getFAQById(id);
        return convertToResponse(faq);
    }

    /**
     * 将FAQ实体转换为响应DTO
     *
     * @param faq FAQ实体
     * @return FAQ响应DTO
     */
    private FAQResponse convertToResponse(FAQ faq) {
        FAQResponse response = new FAQResponse();
        BeanUtils.copyProperties(faq, response);
        response.setCategoryName(faq.getCategoryName());
        return response;
    }
}